package com.qzdsoft.erpcloud.controller.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.act.CenterCompany;
import com.qzdsoft.erpcloud.domain.basic.MeasurementUnit;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.basic.MeasurementUnitService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.query.basic.MeasurementUnitInfo;
import com.qzdsoft.erpcloud.vo.query.basic.MeasurementUnitQueryParam;
import com.qzdsoft.utils.CommonSfEnum;
//import com.qzdsoft.erpcloud.util.SessionUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.basic.BasicMeasurementUnitTypeEnum;
import com.qzdsoft.vo.basic.BasicPreinstallEnum;


/**
 * Created by Admin
 */
@Controller
@RequestMapping("/basic/measurement/unit")
public class MeasurementUnitController extends BaseController{

//    @Autowired
//    private RedisHelper redisHelper;

    @Autowired
    private MeasurementUnitService basicMeasurementUnitService;
    @Autowired
    CenterCompanyService actCenterCompanyService; 
    
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {

//        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
//        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
//        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
//            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
//        }
//
//        List<EnumPo> typeList = new ArrayList<EnumPo>();
//        Map<Object, Object> typeMap = new HashMap<Object, Object>();
//        for(BasicMeasurementUnitTypeEnum e : BasicMeasurementUnitTypeEnum.values()){
//            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
//        }
//
//        List<EnumPo> sfList = new ArrayList<EnumPo>();
//        Map<Object, Object> sfMap = new HashMap<Object, Object>();
//        for(CommonSfEnum e : CommonSfEnum.values()){
//            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
//        }
//
//        model.addAttribute("preinstallList", preinstallList);
//        model.addAttribute("preinstallMap", preinstallMap);
//
//        model.addAttribute("typeList", typeList);
//        model.addAttribute("typeMap", typeMap);
//
//        model.addAttribute("sfList", sfList);
//        model.addAttribute("sfMap", sfMap);
        List<CenterCompany> ls = actCenterCompanyService.getAllCompany();
        model.addAttribute("actCenterCompanyMap",ls);

        return "basic/measurement/index";
    }

    /**
     * 编辑数据
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer CompanyId,Integer id) {
        ModelAndView mv = new ModelAndView();
        
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        List<EnumPo> typeList = new ArrayList<EnumPo>();
        Map<Object, Object> typeMap = new HashMap<Object, Object>();
        for(BasicMeasurementUnitTypeEnum e : BasicMeasurementUnitTypeEnum.values()){
            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> sfList = new ArrayList<EnumPo>();
        Map<Object, Object> sfMap = new HashMap<Object, Object>();
        for(CommonSfEnum e : CommonSfEnum.values()){
            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
        }

        mv.addObject("preinstallList", preinstallList);
        mv.addObject("preinstallMap", preinstallMap);
        mv.addObject("typeList", typeList);
        mv.addObject("typeMap", typeMap);

        mv.addObject("sfList", sfList);
        mv.addObject("sfMap", sfMap);
        List<CenterCompany> ls = actCenterCompanyService.getAllCompany();
        mv.addObject("actCenterCompanyMap",ls);
        
        if(null != id){
            MeasurementUnit  measurementUnit = basicMeasurementUnitService.selectById(id);
            mv.addObject("measurementUnit", measurementUnit);
            mv.setViewName("basic/measurement/edit");
        }else {
            System.out.println("**********************");
            mv.setViewName("basic/measurement/add");
        }
        return mv;
    }

    /**
     * 编辑保存
     * @param request
     * @param basicMeasurementUnit
     * @return
     */
    @ResponseBody
    @SysLog(description="编辑计量单位保存")
    @RequestMapping("/edit_execute")
    public Response<String> editExecute(HttpServletRequest request, MeasurementUnit basicMeasurementUnit) {
//        basicMeasurementUnit.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicMeasurementUnit.getCompany_id()));
        return basicMeasurementUnitService.saveOrUpd(basicMeasurementUnit);
    }
    /**
     * 新增保存
     * @param request
     * @param basicMeasurementUnit
     * @return
     */
    @ResponseBody
    @SysLog(description="新增计量单位保存")
    @RequestMapping("/create_execute")
    public Response<String> createExecute(HttpServletRequest request, MeasurementUnit basicMeasurementUnit) {
//        basicMeasurementUnit.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicMeasurementUnit.getCompany_id()));
        return basicMeasurementUnitService.saveOrUpd(basicMeasurementUnit);
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除计量单位")
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return basicMeasurementUnitService.del(id);
    }

    /**
     * 查询
     * @param request
     * @param param
     * @return
     */
    @ResponseBody
    @SysLog(description="查询计量单位")
    @RequestMapping("/data")
    public LayTableResponse<MeasurementUnitInfo> data(HttpServletRequest request, MeasurementUnitQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
        if(null != param.getCompanyId()){
            param.setCompanyId(param.getCompanyId());
        }

        return basicMeasurementUnitService.selectData(param.getName(), param.getCompanyId(), param.getPage(), param.getLimit());
    }
    
    /**
     * 明细
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) {
        ModelAndView mv = new ModelAndView();
        MeasurementUnit  measurementUnit = basicMeasurementUnitService.selectById(id);
        mv.addObject("measurementUnit", measurementUnit);
        mv.setViewName("basic/measurement/view");
        return mv;
    }

}
